﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XRepair.Models;
using RepositoryUtil.SQL;
using XRepair.Conventions.Enum;

namespace MvcXRepairAdmin.Repository.SQL
{
    public class BrandDAO : BaseDAO
    {
        public IEnumerable<Brand> GetAllActiveBrands()
        {
            string spName = "GetAllActiveBrands";

            DataTable resultTab = null;
            List<Brand> brandList = null;

            using (var conn = XRepairConn)
            {
                conn.Open();

                SqlCommand command = new SqlCommand(spName, conn);
                command.CommandType = CommandType.StoredProcedure;
                resultTab = command.ExecuteDataTable();
            }
            if (resultTab != null && resultTab.Rows.Count > 0)
            {
                brandList = this.ConvertDataTableToBrandList(resultTab);
            }

            return brandList;  

        }

        public bool DeactivateBrandByID(int brandId)
        {
            string spName = "DeactivateBrandByID";

            object Rowcount = null;

            using (var conn = XRepairConn)
            {
                conn.Open();

                SqlCommand command = new SqlCommand(spName, conn);
                command.CommandType = CommandType.StoredProcedure;

                command.AddInParameter("ID", brandId);
                command.AddOutParameter("Rowcount", -1);
                command.ExecuteNonQuery();

                Rowcount = command.GetParameterValue("Rowcount");
            }

            long RowcountValue;

            if (long.TryParse(Rowcount.ToString(), out RowcountValue) && RowcountValue != -1)
            {
                return true;
            }

            return false;
        }

        public bool UpdateBrand(Brand brand)
        {
            string spName = "UpdateBrand";
            object Rowcount = null;

            using (var conn = XRepairConn)
            {
                conn.Open();

                SqlCommand command = new SqlCommand(spName, conn);
                command.CommandType = CommandType.StoredProcedure;

                command.AddInParameter("ID", brand.ID);
                command.AddInParameter("GenericTypesID", brand.GenericTypesID);
                command.AddInParameter("BrandName", brand.BrandName);               
                command.AddInParameter("LastUpdatedAt", brand.LastUpdatedAt);
                command.AddInParameter("LastUpdatedBy", brand.LastUpdatedBy);
                
                command.AddOutParameter("Rowcount", -1);
                command.ExecuteNonQuery();

                Rowcount = command.GetParameterValue("Rowcount");
            }

            long RowcountValue;

            if (long.TryParse(Rowcount.ToString(), out RowcountValue) && RowcountValue != -1)
            {
                return true;
            }

            return false;
        }

        public bool AddNewBrand(Brand brand)
        {
            string spName = "AddNewBrand";
            object newIdentity = null;

            using (var conn = XRepairConn)
            {
                conn.Open();

                SqlCommand command = new SqlCommand(spName, conn);
                command.CommandType = CommandType.StoredProcedure;

                command.AddInParameter("GenericTypesID", brand.GenericTypesID);
                command.AddInParameter("BrandName", brand.BrandName);
                command.AddInParameter("CreatedAt", brand.CreatedAt);
                command.AddInParameter("CreatedBy", brand.CreatedBy);

                command.AddOutParameter("NewIdentity", -1);
                command.ExecuteNonQuery();

                newIdentity = command.GetParameterValue("NewIdentity");
            }

            long newIdentityValue;

            if (long.TryParse(newIdentity.ToString(), out newIdentityValue) && newIdentityValue != -1)
            {
                return true;
            }

            return false;
        }

        private List<Brand> ConvertDataTableToBrandList(DataTable data)
        {
            List<Brand> brands = null;

            if (data != null)
            {
                brands = new List<Brand>();

                foreach (DataRow dr in data.Rows)
                {
                    var serviceModel = new Brand();

                    serviceModel.ID = SqlUtil<int>.GetValue(dr, "ID");
                    serviceModel.GenericTypesID = (DeviceTypes)SqlUtil<int>.GetValue(dr, "GenericTypesID");
                    serviceModel.BrandName = SqlUtil<string>.GetValue(dr, "BrandName");
                    serviceModel.IsActive = SqlUtil<bool>.GetValue(dr, "IsActive");
                    serviceModel.CreatedAt = SqlUtil<DateTime>.GetValue(dr,"CreatedAt");
                    serviceModel.CreatedBy = SqlUtil<String>.GetValue(dr,"CreatedBy");
                    serviceModel.LastUpdatedAt = SqlUtil<DateTime>.GetValue(dr,"LastUpdatedAt");
                    serviceModel.LastUpdatedBy = SqlUtil<String>.GetValue(dr,"LastUpdatedBy");

                    brands.Add(serviceModel);
                }
            }

            return brands;
        }
    }
}
